/**
 * Created by Xiaoming on 2017/8/30.
 */
angular.module('module.wxauth')
    .controller('WxucController', WxucController);
WxucController.$inject = ['$stateParams', '$state','$cookies', 'WxAuthService', 'UserinfoService','AuthService'];
function WxucController($stateParams, $state,$cookies, WxAuthService, UserinfoService,AuthService) {
    // appid
    var vm = this;
    activate();
    vm.back = back;
    vm.wxRegisterThenLogin = wxRegisterThenLogin;
    vm.wxRelationUserThenLogin = wxRelationUserThenLogin;
    vm.getcode = getcode;
    function activate(){
        if ($stateParams.appid) {
            mall_userauth.wxmp_appid = $stateParams.appid;
        }else{
            if($cookies.get("appid")){
                mall_userauth.wxmp_appid = $cookies.get("appid");
            }
        }
        vm.user = {};
        if($stateParams.openid){
            vm.openid = $stateParams.openid;
            vm.wxUser = {
                openid:$stateParams.openid
            };
            userInfoCheck(vm.wxUser);
        }else{
            //获取微信用户信息
            var userinfo = WxAuthService.getUserinfo();
            vm.check = false;//初始化默认用户未注册统一登录
            vm.phoneBind = false;//初始化用户手机未绑定
            if (typeof (userinfo) == 'object' && userinfo.$promise) {//接口回调
                userinfo.$promise.then(function (wxUserInfo) {
                    //获取微信用户信息
                    if(wxUserInfo.webUserDetailRes){
                        vm.wxUser = wxUserInfo.webUserDetailRes;
                    }else if(wxUserInfo.webUserDetailRes == undefined) {
                        vm.wxUser = wxUserInfo;
                    }
                    userInfoCheck(vm.wxUser)
                }, function (error) {
                    vm.error = JSON.stringify(error);
                });
            } else if (typeof (userinfo) == 'string') {
                vm.error = userinfo;
            } else {
                vm.error = "未知错误";
            }
        }
    }
    //回退函数
    function back() {
        setTimeout(function(){
            if($stateParams.shopid && $stateParams.deskid){
                $state.go("store",{shopid:$stateParams.shopid,appid:$stateParams.appid,deskid:$stateParams.deskid});
            }else{
                if($stateParams.openid){
                    window.history.back();
                }else{
                    window.history.go(-2);
                }
            }
        });
    }
    //确认登陆跳回原页面
    function wxRegisterThenLogin() {
        if(vm.check){
            loadCart(true,"leaveLoader",".loader_bac");
            //已经绑定统一登录的用户直接登陆
            UserinfoService.judgeWmUser(mall_userauth.wxmp_appid,vm.wxUser.openid, vm.wxUser.nickname, vm.wxUser.headimgurl,vm.wxUser,vm.wxUser.userid);
            //alert("已经绑定统一登录的用户直接登陆"+JSON.stringify(vm.wxUser));
            if (vm.wxUser) {
                $cookies.put('_userName', vm.wxUser.nickname);
                $cookies.put('_userId', vm.wxUser.userid);
                $cookies.put('_userLogo', vm.wxUser.headimgurl);
                vm.back();
            } else {
                vm.back();
            }
        }/*else{
         //未绑定统一登录的用户
         UserinfoService.wxUserRegister(vm.wxUser.openid, vm.wxUser.nickname, vm.wxUser.headimgurl).$promise.then(function (data) {
         $cookies.put('_userName', vm.wxUser.nickname);
         $cookies.put('_userId', data.userid);
         $cookies.put('_userLogo', vm.wxUser.headimgurl);
         if (vm.wxUser) {
         if($stateParams.shopid && $stateParams.deskid){
         $state.go("store",{shopid:$stateParams.shopid,appid:$stateParams.appid,deskid:$stateParams.deskid});
         }else{
         window.history.go(-3);
         }
         } else {
         window.history.go(-2);
         }
         }, function (error) {
         alert(JSON.stringify(error));
         });
         }*/
    }
    //确认绑定统一登陆账号
    var UserThenLogin = true;
    function wxRelationUserThenLogin(){
        if (!judgePhone(vm.user.phone)) {
            $(".tel").focus();
            $(".tel").select();
            return;
        }
        if(!vm.user.code) {
            input_alert("请输入验证码!");
            return;
        }
        if(vm.check){
            loadCart(true,"leaveLoader",".loader_bac");
            //用户绑定统一登录账号未绑定手机
            //先解除微信绑定
            //UserinfoService.unbinding($stateParams.openid,function(){
            UserinfoService.wxUserRegister(vm.wxUser.openid, vm.wxUser.nickname, vm.wxUser.headimgurl,vm.user).$promise.then(function (data) {
                //alert("用户绑定统一登录账号未绑定手机"+JSON.stringify(data));
                $cookies.put('_userName', vm.wxUser.nickname);
                $cookies.put('_userId', data.userid);
                $cookies.put('_userLogo', vm.wxUser.headimgurl);
                $cookies.put('phone', vm.user.phone);
                vm.back();
            }, function (error) {
                if(error.reason){
                    alert(error.reason.msg);
                }
                loadCart(false,"leaveLoader",".loader_bac");
            });
            //    console.log("解绑成功");
            //},function(){
            //    console.log("解绑失败");
            //});

            /*UserinfoService.bindPhone(vm.user).$promise.then(function (data) {
             $cookies.put('_userName', vm.wxUser.nickname);
             $cookies.put('_userId', vm.wxUser.userid);
             $cookies.put('_userLogo', vm.wxUser.headimgurl);
             vm.back();
             }, function (error) {
             if(error.reason){
             alert(error.reason.msg);
             }
             });*/
        }else{
            loadCart(true,"leaveLoader",".loader_bac");
            //用户未绑定统一登录账号未绑定手机
            UserinfoService.wxUserRegister(vm.wxUser.openid, vm.wxUser.nickname, vm.wxUser.headimgurl,vm.user).$promise.then(function (data) {
                //alert("用户未绑定统一登录账号未绑定手机"+JSON.stringify(data));
                $cookies.put('_userName', vm.wxUser.nickname);
                $cookies.put('_userId', data.userid);
                $cookies.put('_userLogo', vm.wxUser.headimgurl);
                $cookies.put('phone', vm.user.phone);
                vm.back();
            }, function (error) {
                if(error.reason){
                    alert(error.reason.msg);
                }
                loadCart(false,"leaveLoader",".loader_bac");
            });
        }
    }
    //获取验证码
    function getcode(user) {
        if (!judgePhone(user.phone)) {
            $(".tel").focus();
            $(".tel").select();
            return;
        }
        settime();
        AuthService.getcode(user.phone,'wxbind', function () {
        }, function () {
            input_alert("发送短信失败，请重试");
        });
    }
    //用户信息判断
    function userInfoCheck(wxUser){
        //判断微信用户是否绑定统一登录账号
        if(UserinfoService.accountCheck){
            UserinfoService.accountCheck(wxUser,function(user){
                //校验成功用户有绑定统一登录账号;
                wxUser.userid = user.userid;
                UserinfoService.getByUid(user.userid,function(userInfo){
                    if(userInfo.phone){
                        $cookies.put('phone', userInfo.phone);
                        wxUser.phone = userInfo.phone;
                        UserinfoService.judgeWmUser(mall_userauth.wxmp_appid, wxUser.openid, wxUser.nickname, wxUser.headimgurl,wxUser,wxUser.userid);
                        vm.phoneBind = true;//统一登录用户绑定手机
                    }
                },function(){

                });
                vm.check = true;
            },function(){
                //校验失败用户未绑定统一登录账号
                vm.check = false;
            });
        }
    }
}